/**
 * Created by hkf6644 on 2017/5/22.
 */
var orgUploaderObj = {
    uploader : null,
    pickerId : ''
};

var createNewOrgObj = {
    picker1Lis : [],
    picker2Lis : []
};

Vue.component('createNewOrg', {
    props: ['modal','content'],
    template : CommonFunc.ajax.getOuterHtml('component/org_createNewOrg.html'),
    data : function () {
        return createNewOrgObj
    },
    methods: {
        picker1 : function () {
            orgUploaderObj.pickerId = 'picker1';
        },
        picker2 : function () {
            orgUploaderObj.pickerId = 'picker2';
        },
        removeFileLi : function (e) {
            var thisE = $(e.target);
            var id = thisE.closest('li').prop('id');
            var pickerDivId = thisE.closest('ul').siblings('div').prop('id');

            var files = orgUploaderObj.uploader.getFiles();

            orgUploaderObj.uploader.removeFile(files[0], true);

            if (pickerDivId === 'picker1') {
                createNewOrgObj.picker1Lis.splice(0,createNewOrgObj.picker1Lis.length);
            } else if (pickerDivId === 'picker2') {
                createNewOrgObj.picker2Lis.splice(0,createNewOrgObj.picker2Lis.length);
            }
        },
        modalOk : function () {
            if (!vmdata.content.org_manage.parmObj.name) {
                this.$Modal.info({
                    content : '请填写组织名称！'
                });
                return false;
            }

            if (vmdata.modal.type === 'addOrg') {
                if ($('#pickerUl1').find('li').length === 0) {
                    this.$Modal.info({
                        content : '请选择组织logo！'
                    });
                    return false;
                }

                if ($('#pickerUl2').find('li').length === 0) {
                    this.$Modal.info({
                        content : '请选择背景图片！'
                    });
                    return false;
                }
            }

            var server = '';
            var param = null;

            if (vmdata.modal.type === 'addOrg') {
                server = '/cas/console/org/add.ajax';

                param = {
                    name : vmdata.content.org_manage.parmObj.name,
                    loginId : vmdata.content.org_manage.parmObj.loginId,
                    password : vmdata.content.org_manage.parmObj.password
                };
            } else if (vmdata.modal.type === 'editorOrg') {
                server = '/cas/console/org/edit.ajax';

                param = {
                    name : vmdata.content.org_manage.parmObj.name,
                    ouId : vmdata.content.org_manage.parmObj.ouId
                };
            }

            if ($('#pickerUl1').find('li').length > 0 || $('#pickerUl2').find('li').length > 0) {
                orgUploaderObj.uploader.option( 'server', server);

                orgUploaderObj.uploader.on( 'uploadBeforeSend', function( obj, data, headers ) {
                    data.formdata = JSON.stringify(param);
                });
                orgUploaderObj.uploader.upload();
            } else {
                $.ajax({
                    type: "POST",
                    url: '/cas/console/org/editUnImg.ajax',
                    async: true,
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify(param),
                    success: function (rs) {
                        if (rs.isSuccess) {
                            vmdata.successAlert = '修改成功！';
                        } else {
                            vmdata.errorAlert = rs.errMsg;
                        }
                        
                		vmdata.modal.title = '';
				        vmdata.modal.type = '';
				        vmdata.modal.showOrg = false;
                    }
                });
            }
        },
        modalCancel: function() { //浮层-取消
            vmdata.modal.title = '';
	        vmdata.modal.type = '';
	        vmdata.modal.showOrg = false;
            createNewOrgObj.picker1Lis.splice(0,createNewOrgObj.picker1Lis.length);
            createNewOrgObj.picker2Lis.splice(0,createNewOrgObj.picker2Lis.length);
        }
    },
    mounted : function () {
        var _this = this;
        var timer = setTimeout(function () {
            var server = '';

            orgUploaderObj.uploader = WebUploader.create({
                server: server,

                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: {
                    id : '#picker1',
                    multiple : false
                },
                method:"POST",//文件上传方式
                sendAsBinary:false,//是否已二进制的流的方式发送文件，这样整个上传内容php://input都为文件内容
                fileNumLimit: 2,
                fileSizeLimit: 4 * 1024 * 1024,    // 4M
                fileSingleSizeLimit: 2 * 1024 * 1024,    // 2M
                accept: {
                    title: 'Images',
                    extensions: 'gif,jpg,jpeg,bmp,png',
                    mimeTypes: 'image/*'
                }
            });

            orgUploaderObj.uploader.addButton({
                id : '#picker2',
                multiple : false
            });

            orgUploaderObj.uploader.on( 'beforeFileQueued', function( file ) {
                var nameArr = file.name.split('.');
                var houZhui = nameArr[nameArr.length - 1];
                
                if (orgUploaderObj.pickerId === 'picker1') {
                	if (file.name === $('#pickerUl2').children('li').eq(0).attr('file-name')) {
                		vmdata.warningAlert = '不能选择与背景图片相同的图片，请重新选择！';
                		return false;
                	}
                } else if (orgUploaderObj.pickerId === 'picker2') {
                	if (file.name === $('#pickerUl1').children('li').eq(0).attr('file-name')) {
                		vmdata.warningAlert = '不能选择与logo图片相同的图片，请重新选择！';
                		return false;
                	}
                }

                orgUploaderObj.uploader.makeThumb( file, function( error, src ) {
                    if (orgUploaderObj.pickerId === 'picker1') {
                    	var name = file.name;
                        file.name = 'logo.' + houZhui;

                        if ($('#pickerUl1').find('li').length > 0) {
                            var  li1Id = $('#pickerUl1').find('li').eq(0).prop('id');
                            orgUploaderObj.uploader.removeFile(li1Id, true);
                        }

                        createNewOrgObj.picker1Lis.splice(0,createNewOrgObj.picker1Lis.length);
                        createNewOrgObj.picker1Lis.push({
                            id : file.id,
                            src : src,
                            name : name
                        });
                    } else if (orgUploaderObj.pickerId === 'picker2') {
                    	var name = file.name;
                        file.name = 'background.' + houZhui;

                        if ($('#pickerUl2').find('li').length > 0) {
                            var  li2Id = $('#pickerUl2').find('li').eq(0).prop('id');
                            orgUploaderObj.uploader.removeFile(li2Id, true);
                        }

                        createNewOrgObj.picker2Lis.splice(0,createNewOrgObj.picker2Lis.length);
                        createNewOrgObj.picker2Lis.push({
                            id : file.id,
                            src : src,
                            name : name
                        });
                    }
                }, 80, 80);
            });
            orgUploaderObj.uploader.on( 'uploadSuccess', function( file, response ) {
                if (response.isSuccess) {
                    getOrgList.call(_this);

                    if (file.name.search('logo') !== -1) {
                        createNewOrgObj.picker1Lis.splice(0,createNewOrgObj.picker1Lis.length);
                    } else if (file.name.search('background') !== -1) {
                        createNewOrgObj.picker2Lis.splice(0,createNewOrgObj.picker2Lis.length);
                    }

                    if (vmdata.modal.type === 'addOrg') {
                        vmdata.successAlert = '添加成功！';
                    } else if (vmdata.modal.type === 'editorOrg') {
                        vmdata.successAlert = '修改成功！';
                    }
			        
                	vmdata.modal.title = '';
			        vmdata.modal.type = '';
			        vmdata.modal.showOrg = false;
                } else {
                    vmdata.errorAlert = response.errMsg;
                }
            });
            orgUploaderObj.uploader.on( 'uploadError', function( file, errorStr ) {
                vmdata.errorAlert = errorStr
            });
        }, 100);
    }
});